let app = getApp()
import * as echarts from '../../static/ec-canvas/echarts';
let canvas0='';
let width0='';
let height0='';
let dpr0='';
let canvas2='';
let width2='';
let height2='';
let dpr2='';
let dealData=[];
let sexData=[{
  value: 0,
  name: '男'
}, {
  value: 0,
  name: '女'
}];
function initChart(canvas, width, height, dpr) {
  canvas0=canvas;
  width0=width;
  height0=height;
  dpr0=dpr;
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio:dpr
  });
  canvas.setChart(chart);
  var option = {
    backgroundColor: "#ffffff",
    color: ["#3A82F8", "#3AAEF8", "#F8863A", "#4AC54A"],
    tooltip: {
      trigger: 'item',
      formatter: "{a}\n{b} : {c}%"
    },
    legend: {
      bottom: 0,
      data: ['累计访客', '累计客户', '累计意向客户', '累计成交客户']
    },
    calculable: true,
    series: [
      {
        name: '漏斗图',
        type: 'funnel',
        width: '40%',
        height: '80%',
        left: '30%',
        top: '5%',
        label: {
          normal: {
            position: 'left'
          }
        },
        data: dealData
      }
    ]
  };
  chart.setOption(option);
  return chart;
}
function initChart2(canvas, width, height, dpr) {
  canvas2=canvas;
  width2=width;
  height2=height;
  dpr2=dpr;
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  });
  canvas.setChart(chart);
  var option = {
    backgroundColor: "#ffffff",
    color: ["#3a82f8", "#f12d2d"],
    series: [{
      label: {
        normal: {
          fontSize: 14
        }
      },
      type: 'pie',
      center: ['50%', '50%'],
      radius: ['40%', '60%'],
      data:sexData
    }]
  };

  chart.setOption(option);
  return chart;
}

Page({
  data: { 
    sexec: {
      onInit: initChart2
    },
    ec: {
      onInit: initChart
    },
    provinceData: {},
    sexTitle: "",
    sexData: {},
    dealData: [],
    pageData: {},
    cWidth: "",
    cHeight: "",
    pixelRatio: 1,
    pageStatus: 0,
    pageTit: "",
    titShow: !0,
    calendarShow: !1,
    bigTit: [ "总览", "获客排行", "视频排行" ],
    bigCur: 0,
    navArr: [ "今天", "本周", "本月", "全部" ],
    cur: 0,
    params: {
        start_time: "",
        end_time: "",
        type: "today",
        page: 1
    },
    hkData: [],
    videoArr: [],
    nodata: !1,
    circleTitleTop: 135
  },
  choosecalendar:function(){
    if(this.data.titShow==true)
    {
      if(this.data.calendarShow!=true){
        this.setData({
         calendarShow:true
        }) 
      }else{
       this.setData({
          calendarShow:false
       })
     }
    }
    else
    {
      this.setData({
        titShow:true
       }) 
    }
   
  },
  choosetime:function(e){
    var that=this
    that.setData({
      cur:e.currentTarget.dataset.index
    })
    switch(e.currentTarget.dataset.index){
      case 0:
        that.data.params.type='today';
       break
      case 1:
        that.data.params.type='week';
       break
      case 2:
        that.data.params.type='month';
       break
      case 3:
        that.data.params.type='all';
       break
    }
   that.getdata(that.data.bigCur)
  },
  bigtab:function(e){
    this.setData({
      bigCur:e.currentTarget.dataset.index
    })
    this.getdata(e.currentTarget.dataset.index)
  },
  showFunnel: function(t, e) {
        console.log(e)
  },
  getDealDraw: function() {
    if (this.data.dealData = [], 0 != this.data.pageData.visitor_num && this.data.dealData.push({
        name: "累计访客",
        value: this.data.pageData.visitor_num
    }), 0 != this.data.pageData.get_visitor_num && this.data.dealData.push({
        name: "累计客户",
        value: this.data.pageData.get_visitor_num
    }), 0 != this.data.pageData.intention_num && this.data.dealData.push({
        name: "累计意向客户",
        value: this.data.pageData.intention_num
    }), 0 != this.data.pageData.deal_num && this.data.dealData.push({
        name: "累计成交客户",
        value: this.data.pageData.deal_num
    }), this.data.dealData.length) {
      var t = {
        series: this.data.dealData
      };
    }
    dealData=this.data.dealData
    this.setData({
      dealData:dealData
    })
    initChart(canvas0,width0,height0,dpr0)
},
  getdata(index){
     var that=this
     if(index==0)
     {
      if(this.data.optionsid==0){
        wx.request({
          url: app.globalData.siteroot,
           data: {
             do:'Member_statistics',
             type:that.data.params.type,
             staff_id:wx.getStorageSync('staff_bind_id'),
             start_time:that.data.params.start_time,
             end_time:that.data.params.end_time,
          },
         success: function(res) {
             that.setData({
              pageData:res.data.info
             })
             that.getDealDraw()
             sexData = [ {
              name: "男",
              value: res.data.info.man_num
          }, {
              name: "女",
              value: res.data.info.woman_num
          } ]
          initChart2(canvas2,width2,height2,dpr2)
         }
        })
      }else{
        wx.request({
          url: app.globalData.siteroot,
           data: {
             do:'Member_totalStatistics',
             type:that.data.params.type,
             staff_id:wx.getStorageSync('staff_bind_id'),
             start_time:typeof(that.data.params.start_time)!='undefined'?that.data.params.start_time:'',
             end_time:typeof(that.data.params.end_time)!='undefined'?that.data.params.end_time:'',
          },
         success: function(res) {
             that.setData({
              pageData:res.data.info
             })
             that.getDealDraw()
             sexData = [ {
              name: "男",
              value: res.data.info.man_num
             }, {
              name: "女",
              value: res.data.info.woman_num
             } ]
             initChart2(canvas2,width2,height2,dpr2)
         }
        })
      }
     }
     if(index==1)
     {
      wx.request({
        url: app.globalData.siteroot,
         data: {
           do:'Member_getUserRank',
           type:that.data.params.type,
           staff_id:wx.getStorageSync('staff_bind_id'),
           start_time:that.data.params.start_time,
           end_time:that.data.params.end_time,
        },
       success: function(res) {
           that.setData({
            hkData:res.data.info
           })
       }
      })
     }
     if(index==2)
     {
      wx.request({
        url: app.globalData.siteroot,
         data: {
           do:'Member_videoRank',
           type:that.data.params.type,
           staff_id:wx.getStorageSync('staff_bind_id'),
           start_time:that.data.params.start_time,
           end_time:that.data.params.end_time,
        },
       success: function(res) {
           that.setData({
            videoArr:res.data.info
           })
       }
      })
     }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  chooseday:function(e) {
    var params=this.data.params
    params['type']='section'
    params['start_time']=e.detail.starttime
    params['end_time']=e.detail.endtime
    this.setData({
      params:params
    })
    this.getdata(this.data.bigCur)
    this.setData({
      titShow:false
    })
    

  },
  onLoad: function (options) {
    var that=this 
    if(options.type=='video')
    {
      wx.setNavigationBarTitle({
        title:'boss中心',
      })
      this.setData({
        pageStatus:1,
        optionsid:1,
        bigCur:0
      })
    }else{
      if(options.boss==0)
      {
        wx.setNavigationBarTitle({
          title:'boss中心'
        })
        that.setData({
          pageStatus:options.status,
          bigCur:0,
          optionsid:1
        })
      }
      else
      {
        if(options.boss==1){
          that.setData({
            pageStatus:0,
            bigCur:0,
            optionsid:0
          })
        }else{
          that.setData({
            pageStatus:options.status,
            bigCur:2,
            optionsid:0
          })
        }
      }
    }
  
    this.getdata(that.data.bigCur)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  shareset:function(e){
    console.log(e.detail)
 },
 onShareAppMessage: function (e) {
   return console.log(e, "页面内按钮分享"), {
     title:e.target.dataset.name,
     imageUrl:e.target.dataset.img,
     path:"/business-service/pages/staff_newIndex/index"
 };
 }
})